import {createRouter, createWebHashHistory} from 'vue-router';

import Login from "../views/Login.vue";
import BusinessList from "../components/customer/BusinessList.vue";
import CustomerOrders from "../components/customer/CustomerOrders.vue";
import BusinessInfo from "../components/customer/BusinessInfo.vue";
import ItemInfo from "../components/customer/ItemInfo.vue";
import Confirm from "../components/customer/Confirm.vue";
import CustomerManage from "../views/CustomerManage.vue";
import BusinessManage from "../views/BusinessManage.vue";
import ShopList from "../components/business/ShopList.vue";
import BusinessOrders from "../components/business/BusinessOrders.vue";
import ItemList from "../components/business/ItemList.vue";
import ShopInfo from "../components/business/ShopInfo.vue";
import OrderDetail from "../components/customer/CustomerOrderDetail.vue";
import BusinessOrderDetail from "../components/business/BusinessOrderDetail.vue";


const routes = [
    {
      path: "/",
      redirect:"/index/businessList"
    },
    {
        path: "/manage",
        redirect: "/manage/shopList"
    },
    {
        path: "/index",
        name: "index",
        component: CustomerManage,
        children:[
            {
                path: "businessList",
                name: "店铺列表",
                component: BusinessList
            },
            {
                path: "customerOrders",
                name: "我的订单",
                component: CustomerOrders
            },
            {
                path: "businessInfo",
                name: "商户信息",
                component: BusinessInfo
            },
            {
                path: "orderDetail",
                name:"订单明细",
                component: OrderDetail
            }
            // {
            //     path: "itemInfo",
            //     name: "商品信息",
            //     component: ItemInfo
            // },
            // {
            //     path: "confirm",
            //     name:"确认订单",
            //     component: Confirm
            // }
        ]
    },
    {
        path: "/manage",
        name: "manage",
        component:BusinessManage,
        children: [
            {
                path: "shopList",
                name: "门店列表",
                component: ShopList
            },
            {
                path: "itemList",
                name: "商品列表",
                component: ItemList
            },
            {
                path: "businessOrders",
                name: "订单列表",
                component: BusinessOrders
            },
            {
                path: "shopInfo",
                name:"门店信息",
                component: ShopInfo
            },
            {
                path: "orderDetail",
                name: "订单信息",
                component: BusinessOrderDetail
            }

        ]
    },
    {
        path:"/login",
        name:"login",
        component:Login
    },



]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach((to, from, next) => {


    /***************检测是否有token，若无则返回登录页*****************/
    const user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : null
    const isLogin = !!user
    // //放行登录页和注册页
    // if (to.path === '/login' ) {
    //     next()
    // }else  {
    //     isLogin ? next() : next('/login')
    // }
    // Allow access to login page and registration page
    if (to.path === '/login') {
        next();
    } else {
        // If user is not logged in, redirect to login page
        if (!isLogin) {
            next('/login');
        } else {
            // Check user type and route accordingly
            const userType = user.type;

            if (to.path.startsWith('/index') && userType === 0) {
                // User is a customer, allow access to customer routes
                next();
            } else if (to.path.startsWith('/manage') && userType === 1) {
                // User is a merchant, allow access to merchant routes
                next();
            } else {
                // User is trying to access the opposite type of route, redirect to appropriate page
                if (userType === 0) {
                    next('/index/businessList');
                } else if (userType === 1) {
                    next('/manage/shopList');
                }
            }
        }
    }
})

export default router